#!/bin/bash

source ../config/config.cfg

lpwd=`pwd`

function check()
{
	checkenvironment
	checkworkspace
}

function unzipmysqlpackage()
{
	mkdir -p ${TMP_DIR}
	echo "解压MySQL安装介质..."
	fullfilename=`ls ${INSTALL_DIR}/data/${MYSQL_DIR}/${MYSQL_NAME}.*`
	filename=$(basename "$fullfilename")
	fname="${filename%.*}"
	extname="${filename##*.}"
	if [ ${extname} == "xz" ]; then
		tar -Jxf ${INSTALL_DIR}/data/${MYSQL_DIR}/${filename} -C ${TMP_DIR}/
	elif [ ${extname} == "gz" ]; then
		tar xf ${INSTALL_DIR}/data/${MYSQL_DIR}/${filename} -C ${TMP_DIR}/
	elif [ ${extname} == "tar" ]; then
		tar xf ${INSTALL_DIR}/data/${MYSQL_DIR}/${filename} -C ${TMP_DIR}/
	fi
	sleep 1
}

function modifyconfigfile()
{
	echo "生成my.cnf配置文件..."
	echo "# For advice on how to change settings please see" > ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# *** default location during install, and will be replaced if you" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# *** upgrade to a newer version of MySQL." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "[client]" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "default-character-set=utf8" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "[mysqld]" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "default_authentication_plugin=mysql_native_password" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "character-set-server=utf8" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "collation-server=utf8_general_ci" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "default_password_lifetime=0" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# Remove leading # and set to the amount of RAM for the most important data" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "skip-host-cache" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "skip-name-resolve" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "skip-log-bin" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "lower_case_table_names=1" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "skip-ssl" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "default-time-zone='+8:00'" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# Remove leading # to turn on a very important data integrity option: logging" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# changes to the binary log between backups." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# log_bin" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# These are commonly set, remove the # and set as required." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "basedir=workspace_path/mysql_name" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "datadir=workspace_path/mysql_name/data" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "socket=workspace_path/mysql_name/mysql.sock" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "port=port_num" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "mysqlx=0" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "#server_id=" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# Remove leading # to set options mainly useful for reporting servers." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# The server defaults are faster for transactions and fast SELECTs." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# Adjust sizes as needed, experiment to find the optimal values." >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# join_buffer_size=128M" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# sort_buffer_size=2M" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "# read_rnd_buffer_size=2M" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "max_allowed_packet=300M" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "max_connections=1000" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "innodb_lock_wait_timeout=500" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "innodb_buffer_pool_size=${MYSQL_INNODB_BUFFER_POOL_SIZE}M" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "innodb_print_all_deadlocks=1" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	echo "log-error=workspace_path/mysql_name/error.err" >> ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	sed -i -e "s:workspace_path:${workspace}:g" -e "s/mysql_name/${MYSQL_NAME}/g" -e "s/port_num/${MYSQL_PORT}/g" ${TMP_DIR}/${MYSQL_NAME}/my.cnf
	mkdir -p ${TMP_DIR}/${MYSQL_NAME}/data
	echo "生成启动脚本文件..."
	echo "#!/bin/bash" > ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	echo "echo \">>>>>> Start MySQL...\"" >> ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	echo "${MYSQL_HOME}/bin/mysqld --defaults-file=${MYSQL_HOME}/my.cnf >${MYSQL_HOME}/mysql.log 2>&1 >/dev/null &" >> ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	echo "sleep 20" >> ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	echo "echo \">>>>>> MySQL Start completed!\"" >> ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	echo "#!/bin/bash" > ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "echo \">>>>>> Stop MySQL...\"" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "${MYSQL_HOME}/bin/mysqladmin -uroot -p${MYSQL_PASSWORD} --socket ${MYSQL_HOME}/mysql.sock shutdown" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "sleep 20" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "ps -ef | grep mysql | grep ${MYSQL_HOME} | grep -v stop-mysql.sh | grep -v grep | awk '{print \$2}' | while read pid" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "do" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "         kill -9 \${pid}" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "done" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "echo \">>>>>> MySQL Stop completed!\"" >> ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	echo "#!/bin/bash" > ${TMP_DIR}/${MYSQL_NAME}/cd-mysql.sh
	echo "${MYSQL_HOME}/bin/mysql -u${MYSQL_USERNAME} -p${MYSQL_PASSWORD} --socket ${MYSQL_HOME}/mysql.sock" >> ${TMP_DIR}/${MYSQL_NAME}/cd-mysql.sh
	chmod +x ${TMP_DIR}/${MYSQL_NAME}/start-mysql.sh
	chmod +x ${TMP_DIR}/${MYSQL_NAME}/stop-mysql.sh
	chmod +x ${TMP_DIR}/${MYSQL_NAME}/cd-mysql.sh
	sleep 1
}

function dispatchmysql()
{
	echo "分发${MYSQL_NAME}到服务器${MYSQL_IP}"
	cd ${TMP_DIR}
	mv ${TMP_DIR}/${MYSQL_NAME} ${workspace}
	cd ${lpwd}
	sleep 1
}

function initializemysql()
{
	echo "初始化MySQL..."
	rm -rf ${MYSQL_HOME}/data
	rm -f ${MYSQL_HOME}/error.err
	rm -f ${MYSQL_HOME}/mysql.log
	mkdir -p ${MYSQL_HOME}/data
	${MYSQL_HOME}/bin/mysqld --defaults-file=${MYSQL_HOME}/my.cnf --initialize-insecure
}

function startmysql()
{
	echo ">>>>>> Start MySQL..."
	${MYSQL_HOME}/bin/mysqld --defaults-file=${MYSQL_HOME}/my.cnf >${MYSQL_HOME}/mysql.log 2>&1 >/dev/null &
	sleep 20
	${MYSQL_HOME}/bin/mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${MYSQL_PASSWORD}';create user 'root'@'%' identified by '${MYSQL_PASSWORD}';GRANT all privileges ON *.* TO 'root'@'%' WITH GRANT OPTION;alter user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '${MYSQL_PASSWORD}';flush privileges;" --socket=${MYSQL_HOME}/mysql.sock
	echo ">>>>>> MySQL Start completed!"
}

function generatescript()
{
        echo "生成启动脚本文件..."
	if [ ! -d ${workspace}/${MYSQL_NAME} ]; then
		mkdir -p ${workspace}/${MYSQL_NAME}
	fi
	echo "#!/bin/bash" > ${workspace}/${MYSQL_NAME}/start-mysql.sh
       	echo "echo \">>>>>> Start MySQL...\"" >> ${workspace}/${MYSQL_NAME}/start-mysql.sh
	echo "${MYSQL_HOME}/bin/mysqld --defaults-file=${MYSQL_HOME}/my.cnf >${MYSQL_HOME}/mysql.log 2>&1 >/dev/null &" >> ${workspace}/${MYSQL_NAME}/start-mysql.sh
       	echo "sleep 20" >> ${workspace}/${MYSQL_NAME}/start-mysql.sh
       	echo "echo \">>>>>> MySQL Start completed!\"" >> ${workspace}/${MYSQL_NAME}/start-mysql.sh
       	chmod +x ${MYSQL_HOME}/start-mysql.sh
	echo "#!/bin/bash" > ${workspace}/${MYSQL_NAME}/stop-mysql.sh
	echo "echo \">>>>>> Stop MySQL...\"" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "${MYSQL_HOME}/bin/mysqladmin -uroot -p${MYSQL_PASSWORD} --socket ${MYSQL_HOME}/mysql.sock shutdown" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "sleep 20" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "ps -ef | grep mysql | grep ${MYSQL_HOME} | grep -v stop-mysql.sh | grep -v grep | awk '{print \$2}' | while read pid" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "do" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "         kill -9 \${pid}" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "done" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	echo "echo \">>>>>> MySQL Stop completed!\"" >> ${workspace}/${MYSQL_NAME}/stop-mysql.sh
       	chmod +x ${workspace}/${MYSQL_NAME}/stop-mysql.sh
	count=`cat ${workspace}/start.sh | grep start-mysql.sh | wc -l`
       	if [ 0 == $count ]; then
              		echo  "${workspace}/${MYSQL_NAME}/start-mysql.sh" >> ${workspace}/start.sh
	fi
	count=`cat ${workspace}/stop.sh | grep stop-mysql.sh | wc -l`
       	if [ 0 == $count ]; then
               	echo "${workspace}/${MYSQL_NAME}/stop-mysql.sh" >> ${workspace}/stop.sh
	fi
}

starttime=`date +'%Y-%m-%d %H:%M:%S'`
check
unzipmysqlpackage
modifyconfigfile
dispatchmysql
initializemysql
startmysql
generatescript

echo "  "
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间： "$((end_seconds-start_seconds))"s"
